<?php

namespace App\Admin\Controllers;

use App\MealsJoin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class MealsJoinController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '饭点打卡明细';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new MealsJoin());
        $grid->model()->orderBy('id', 'desc');

        $grid->filter(function($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 设置datetime类型
            $filter->equal('id', '编号');
            $filter->equal('uid', '用户id');
            $filter->like('user.phone', '买家手机号');
            $filter->where(function ($query) {
                switch ($this->input) {
                    case 'yes':
                        $query->where('get_amount', '>', 0);
                        break;
                    case 'no':
                        $query->where('get_amount', '=', 0);
                        break;
                }
            }, '是否完成')->radio([
                'yes' => '已完成',
                'no' => '未完成',
            ]);


        });

        $grid->column('id', '报名编码');
        $grid->column('user.name', '用户名')->display(function($value){
            return $value.'<br/>'.$this->user->phone;
        });
        $grid->column('amount', '平分资格');
        $grid->column('status', '打卡状态')->display(function($value){
            return $this->get_amount>0?'已完成':'未完成';
        });
        $grid->column('created_at', '报名时间');
        $grid->column('updated_at', '签到时间');
        $grid->column('get_amount', '分得品鉴金');
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(MealsJoin::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('uid', __('Uid'));
        $show->field('amount', __('Amount'));
        $show->field('date', __('Date'));
        $show->field('is_sign', __('Is sign'));
        $show->field('get_amount', __('Get amount'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new MealsJoin());

        $form->number('uid', __('Uid'));
        $form->number('amount', __('Amount'));
        $form->date('date', __('Date'))->default(date('Y-m-d'));
        $form->switch('is_sign', __('Is sign'));
        $form->decimal('get_amount', __('Get amount'))->default(0.00);

        return $form;
    }
}
